/**
 * @param {Object} options
 * url:地址
 * method:请求方式，默认get
 * data:请求参数
 * header:请求头
 * isShowLoading: 是否显示加载提示 默认不显示，需要显示传true
 * lodingText:提示文字，默认 处理中...
 * timeout:超时时间，默认10000
 * 
 * 使用示例：
 * this.$request({
			url:'http://www.seereals.com',
			methods:'get',
			data:{
				name:'zhangsan',
				age:'24'
			},
			isShowLoading:true,
			lodingText:'加载中...'
		}).then(res=>{
			console.log(res);
		}).catch(err=>{
			console.log(err);
		});
 */
export default function request(options) {
	const {
		url,
		method,
		data,
		header,
		isShowLoading,
		lodingText,
		timeout
	} = options
	if (isShowLoading) {
		uni.showLoading({
			title: lodingText || '处理中...'
		});
	}
	// header[content-type] = "application/json";
	// header['Authorization'] = 'Bearer ' + uni.getStorageSync('token');
	
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: data || {},
			method: method || 'GET',
			// header: header || {"content-type": "application/x-www-form-urlencoded"},
			// header: header || {"content-type": "application/json","Authorization":"Bearer "+uni.getStorageSync('token')},
			header: header || {"content-type": "application/json"},
			timeout: timeout || 10000, // 请求超时时间设置
			success: (res) => {
				uni.hideLoading() 
				resolve(res.data)
			},
			fail: (err) => {
				uni.showToast({
					title: '网络错误，请稍后再试',
					icon: 'none'
				})
				uni.hideLoading() 
				reject(err)
			},
			complete: (err) => {
				if (isShowLoading) {
					uni.hideLoading() // 请求完成后不管成功失败都关闭loading
				}
				reject(err)
			}
		})
	})
}
